package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.AssetBrokenInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Asset broken fragment.
 *
 * @author sHadowLess
 */
public class AssetBrokenFragment extends BaseFragment {

    /**
     * The Broken order no.
     */
    @BindView(R.id.broken_orderNo)
    TextView brokenOrderNo;
    /**
     * The Broken make time.
     */
    @BindView(R.id.broken_makeTime)
    TextView brokenMakeTime;
    /**
     * The Broken maker.
     */
    @BindView(R.id.broken_maker)
    TextView brokenMaker;
    /**
     * The Broken make position.
     */
    @BindView(R.id.broken_makePosition)
    TextView brokenMakePosition;
    /**
     * The Broken apply depart.
     */
    @BindView(R.id.broken_applyDepart)
    TextView brokenApplyDepart;
    /**
     * The Broken asset id.
     */
    @BindView(R.id.broken_assetId)
    TextView brokenAssetId;
    /**
     * The Broken asset name.
     */
    @BindView(R.id.broken_assetName)
    TextView brokenAssetName;
    /**
     * The Broken model.
     */
    @BindView(R.id.broken_model)
    TextView brokenModel;
    /**
     * The Broken use date.
     */
    @BindView(R.id.broken_useDate)
    TextView brokenUseDate;
    /**
     * The Broken origin cost.
     */
    @BindView(R.id.broken_originCost)
    TextView brokenOriginCost;
    /**
     * The Broken cut year.
     */
    @BindView(R.id.broken_cutYear)
    TextView brokenCutYear;
    /**
     * The Broken month cut value.
     */
    @BindView(R.id.broken_monthCutValue)
    TextView brokenMonthCutValue;
    /**
     * The Broken add cut value.
     */
    @BindView(R.id.broken_addCutValue)
    TextView brokenAddCutValue;
    /**
     * The Broken only value.
     */
    @BindView(R.id.broken_onlyValue)
    TextView brokenOnlyValue;
    /**
     * The Broken broken reason.
     */
    @BindView(R.id.broken_brokenReason)
    TextView brokenBrokenReason;
    /**
     * The Broken remark.
     */
    @BindView(R.id.broken_remark)
    TextView brokenRemark;
    /**
     * The Broken check process.
     */
    @BindView(R.id.broken_check_process)
    LinearLayout brokenCheckProcess;

    private final String url = "api/Scrap/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_asset_broken_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initBrokenInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, brokenCheckProcess);
        }
    }

    private void initBrokenInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getAssetBrokenInfo(orderId, userName, new RequestCallBack<AssetBrokenInfo>() {
                    @Override
                    public void success(RequestResult<AssetBrokenInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                AssetBrokenInfo.DataBean dataBean = result.getResult().getData();
                                AssetBrokenInfo.DataBean.AssetBean assetBean = dataBean.getAsset();

                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String createUserName = dataBean.getCreateUserName();
                                brokenOrderNo.setText(id);
                                brokenMakeTime.setText(createTimeStr);
                                brokenMaker.setText(createUserName);

                                String assetNumber = assetBean.getAssetNumber();
                                String assetName = assetBean.getAssetName();
                                String modelNumber = assetBean.getModelNumber();
                                String startDateStr = assetBean.getStartDateStr();
                                String originalValue = getDecimal(assetBean.getOriginalValue().doubleValue());
                                String depreciationYear = getDecimal(dataBean.getDepreciationYear().doubleValue());
                                String depreciationMonth = getDecimal(dataBean.getDepreciationMonth().doubleValue());
                                String depreciationTotal = getDecimal(dataBean.getDepreciationTotal().doubleValue());
                                String leftValue = getDecimal(dataBean.getLeftValue().doubleValue());
                                String scrapReason = dataBean.getScrapReason();
                                String remark = dataBean.getRemark();
                                brokenAssetId.setText(assetNumber);
                                brokenAssetName.setText(assetName);
                                brokenModel.setText(modelNumber);
                                brokenUseDate.setText(startDateStr);
                                brokenOriginCost.setText(originalValue);
                                brokenCutYear.setText(depreciationYear);
                                brokenMonthCutValue.setText(depreciationMonth);
                                brokenAddCutValue.setText(depreciationTotal);
                                brokenOnlyValue.setText(leftValue);
                                brokenBrokenReason.setText(scrapReason);
                                brokenRemark.setText(remark);
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }

    @Override
    protected void initView() {

    }
}
